package com.bobo.medicinechest.manager;

import com.blankj.utilcode.util.ToastUtils;
import com.bobo.medicinechest.bean.GoodsBean;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class CartManager {
    private static CartManager instance;
    private List<GoodsBean> carList;
    
    private CartManager() {
        carList = new ArrayList<>();
    }
    
    public static CartManager getInstance() {
        if (instance == null) {
            instance = new CartManager();
        }
        return instance;
    }
    
    /**
     * 添加商品到购物车
     * @param goods 要添加的商品
     */
    public void addGoodsToCart(GoodsBean goods) {
        if (goods == null || goods.getId() == null) {
            return;
        }
        
        // 检查购物车中是否已存在该商品
        for (GoodsBean cartGoods : carList) {
            if (cartGoods.getId().equals(goods.getId())) {
                // 商品已存在，数量+1
                cartGoods.setNumber(cartGoods.getNumber() + 1);
                return;
            }
        }
        
        // 商品不存在，添加到购物车
        GoodsBean newGoods = new GoodsBean();
        newGoods.setId(goods.getId());
        newGoods.setName(goods.getName());
        newGoods.setPrice(goods.getPrice());
        newGoods.setNumber(1);
        carList.add(newGoods);
    }

    /**
     * 减少商品数量 (为1时只提示，不删除)
     * @param goodsId 商品ID
     */
    public void minusGoodsNumber(String goodsId) {
        if (goodsId == null) {
            return;
        }
        for (int i = 0; i < carList.size(); i++) {
            GoodsBean goods = carList.get(i);
            if (goods.getId().equals(goodsId)) {
                int currentNumber = goods.getNumber();
                if (currentNumber > 1) {
                    // 数量大于1，减少1
                    goods.setNumber(currentNumber - 1);
                } else {
                    // 数量为1，移除商品
                    ToastUtils.showLong("购买数量不能少于1哟~");
                }
                break;
            }
        }
    }

    /**
     * 减少商品数量
     * @param goodsId 商品ID
     */
    public void decreaseGoodsNumber(String goodsId) {
        if (goodsId == null) {
            return;
        }

        for (int i = 0; i < carList.size(); i++) {
            GoodsBean goods = carList.get(i);
            if (goods.getId().equals(goodsId)) {
                int currentNumber = goods.getNumber();
                if (currentNumber > 1) {
                    // 数量大于1，减少1
                    goods.setNumber(currentNumber - 1);
                } else {
                    // 数量为1，移除商品
                    carList.remove(i);
                }
                break;
            }
        }
    }

    
    /**
     * 从购物车移除商品
     * @param goodsId 商品ID
     */
    public void removeGoodsFromCart(String goodsId) {
        for (int i = 0; i < carList.size(); i++) {
            if (carList.get(i).getId().equals(goodsId)) {
                carList.remove(i);
                break;
            }
        }
    }
    
    /**
     * 更新购物车中商品数量
     * @param goodsId 商品ID
     * @param number 新的数量
     */
    public void updateGoodsNumber(String goodsId, int number) {
        for (GoodsBean goods : carList) {
            if (goods.getId().equals(goodsId)) {
                goods.setNumber(number);
                break;
            }
        }
    }
    
    /**
     * 获取购物车商品列表
     * @return 购物车列表
     */
    public List<GoodsBean> getCartList() {
        return new ArrayList<>(carList);
    }
    
    /**
     * 获取购物车中商品总数量
     * @return 总数量
     */
    public int getTotalCount() {
        int total = 0;
        for (GoodsBean goods : carList) {
            total += goods.getNumber();
        }
        return total;
    }
    
    /**
     * 获取购物车总金额
     * @return 总金额
     */
//    public double getTotalPrice() {
//        double total = 0;
//        for (GoodsBean goods : carList) {
//            total += Double.valueOf(goods.getPrice()) * goods.getNumber();
//
//        }
//        return total;
//    }

    public String getTotalPrice() {
        BigDecimal total = BigDecimal.ZERO;
        for (GoodsBean goods : carList) {
            // 使用字符串构造BigDecimal避免精度问题
            BigDecimal price = new BigDecimal(goods.getPrice().toString());
            BigDecimal quantity = new BigDecimal(goods.getNumber());

            // 乘法计算并累加
            BigDecimal itemTotal = price.multiply(quantity);
            total = total.add(itemTotal);
        }
        return total.toString();
//        // 格式化为字符串，保留2位小数，四舍五入
//        return total.setScale(2, RoundingMode.HALF_UP).toString();
    }
    
    /**
     * 清空购物车
     */
    public void clearCart() {
        carList.clear();
    }
    
    /**
     * 检查商品是否在购物车中
     * @param goodsId 商品ID
     * @return 是否存在
     */
    public boolean isGoodsInCart(String goodsId) {
        for (GoodsBean goods : carList) {
            if (goods.getId().equals(goodsId)) {
                return true;
            }
        }
        return false;
    }
}